package com.enterprise.projectManager.resource;


import com.enterprise.projectManager.dto.CheckSearchConditions;

import com.enterprise.projectManager.model.QualityCheck;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.QualityCheckImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/crebas/quality")
@RequiredArgsConstructor
public class QualityCheckResource {
    private final QualityCheckImpl qualityCheckService;
    // 创建一个新的质量检测
    @PostMapping("/createQuality")
    public ResponseEntity<Response> createQuality(@RequestBody QualityCheck quality) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("newQuality", qualityCheckService.createQual(quality)))
                        .message("新建质量检测")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );
    }
    // 删除一个质量检测
    @DeleteMapping("/deleteQuality/{id1}")
    public ResponseEntity<Response> deleteQuality(@PathVariable String id1) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("deleteResult",qualityCheckService.deleteQual(id1)))
                        .message("删除质量检测")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    // 更新一个质量检测
    @PostMapping("/updateQuality")
    public ResponseEntity<Response> updateQuality(@RequestBody QualityCheck quality) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updatedQuality",qualityCheckService.updateQual(quality)))
                        .message("更新质量检测")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有质量检测
    @GetMapping("/listQuality/{limitqual}")
    public ResponseEntity<Response> listQualitys(@PathVariable int limitqual) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("qualities",qualityCheckService.listQual(limitqual)))
                        .message("获取"+limitqual+"个质量检测")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有质量检测
    @GetMapping("/listAll")
    public ResponseEntity<Response> listAll() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("qualities",qualityCheckService.listAll()))
                        .message("获取所有质量检测")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }


    // 过滤查询质量检测
    @PostMapping("/filterQualityCheck")
    public ResponseEntity<Response> filterQualityCondition(@RequestBody CheckSearchConditions checkSearchConditions) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("filterQualityCheck",qualityCheckService.filterQualityCheckBySQL(checkSearchConditions)))
                        .message("查找质量检测")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }



}
